﻿using Simple.Tools.Entity;
using Simple.Tools.Extensions;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Simple.Tools.Map.Check
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{

	internal class BoolCheckType: BaseCheckType
	{
		public BoolCheckType(BaseCheckType next) : this()
		{
			this.next = next;
		}
		public BoolCheckType() : base(typeof(bool))
		{
			nullableType = typeof(Nullable<bool>);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="source"></param>
		/// <param name="member"></param>
		/// <returns></returns>
		public override bool IsNull(object source, PropertyInfo member)
		{

			if (PropertyEqualsType(member))
			{
				object obj = member.GetValue(source);
				if (SimpleCheck.IsNull(obj))
				{
					return true;
				}
				return Verify(obj.ToBool());
			}
			if (next != null)
			{
				return next.IsNull(source, member);
			}
			return base.IsNull(source, member);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="source"></param>
		/// <param name="member"></param>
		/// <returns></returns>
		public override bool IsNull(object source, FieldInfo member)
		{
			if (FieldEqualsType(member))
			{
				object obj = member.GetValue(source);
				if (SimpleCheck.IsNull(obj))
				{
					return true;
				}
				return Verify(obj.ToBool());
			}

			return base.IsNull(source, member);
		}
		public bool Verify(bool b)
		{
			return false;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="map"></param>
		/// <param name="pSource"></param>
		/// <param name="pTarget"></param>
		/// <param name="sourceValue"></param>
		/// <param name="Target"></param>
		public override void SetValue(MapToAttribute map, PropertyInfo pSource, PropertyInfo pTarget, object sourceValue, object Target)
		{
			base.SetValue(map, pSource, pTarget, sourceValue, Target);
			if (EqualsType(map.TargetType))
			{
				bool lvalue = sourceValue.ToBool();
				pTarget.SetValue(Target, lvalue);
			}
			else
			{
				if (next != null)
				{
					next.SetValue(map, pSource, pTarget, sourceValue, Target);
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="map"></param>
		/// <param name="pSource"></param>
		/// <param name="pTarget"></param>
		/// <param name="sourceValue"></param>
		/// <returns></returns>
		public override object GetTargetValue(MapToAttribute map, Type pSource, Type pTarget, object sourceValue)
		{
			base.SetValue(map, pSource, pTarget, sourceValue);
			if (EqualsType(map.TargetType))
			{
				return sourceValue.ToBool();
			}
			else
			{
				if (next != null)
				{
					return next.GetTargetValue(map, pSource, pTarget, sourceValue);
				}
			}
			return null;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="map"></param>
		/// <param name="pSource"></param>
		/// <param name="pTarget"></param>
		/// <param name="sourceValue"></param>
		/// <param name="Target"></param>
		public override void SetValue(MapToAttribute map, FieldInfo pSource, FieldInfo pTarget, object sourceValue, object Target)
		{
			base.SetValue(map, pSource, pTarget, sourceValue, Target);
			if (EqualsType(map.TargetType))
			{
				bool lvalue = sourceValue.ToBool();
				pTarget.SetValue(Target, lvalue);
			}
			else
			{
				if (next != null)
				{
					next.SetValue(map, pSource, pTarget, sourceValue, Target);
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="property"></param>
		/// <param name="value"></param>
		/// <param name="target"></param>
		public override void SetValue(PropertyInfo property, object value, object target)
		{
			if (PropertyEqualsType(property))
			{
				property.SetValue(value, value.ToBool());
			}
			else
			{
				if (next != null)
				{
					next.SetValue(property, value, target);
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="fieldinfo"></param>
		/// <param name="value"></param>
		/// <param name="target"></param>
		public override void SetValue(FieldInfo fieldinfo, object value, object target)
		{
			if (FieldEqualsType(fieldinfo))
			{
				fieldinfo.SetValue(value, value.ToBool());
			}
			else
			{
				if (next != null)
				{
					next.SetValue(fieldinfo, value, target);
				}
			}
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="prop"></param>
		/// <param name="max"></param>
		/// <param name="min"></param>
		/// <returns></returns>
		public override KeyValue<bool, string> VerifyRange(object value, PropertyInfo prop, double max, double min)
		{
			KeyValue<bool, string> result = new KeyValue<bool, string>() { Key=true };

			if (PropertyEqualsType(prop))
			{
				if (value!=null)
				{
					bool dValue = value.ToBool();
					result.Value = "验证通过!";
				}
				else
				{
					result.Key = false;
					result.Value = "验证失败";
				}
			
			}
			else if (next != null)
			{
				return next.VerifyRange(value, prop, max, min);
			}
			else
			{
				return base.VerifyRange(value, prop, max, min);
			}

			return result;
		}
	}
}
